package com.itheima.health.controller;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查项控制层
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;
    /**
     * 添加检查项
     */
    //RequestMapping表示设置请求路径
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验
    @RequestMapping("/add.do")
    //RequestBody 表示返回结果为json数据
    //注解加到类上面表示类返回为json
    public Result add(@RequestBody CheckItem checkItem) {//将前端返回的json数据转换为java类型
        //1. 调用业务层
        Boolean flag = checkItemService.add(checkItem);
        if (flag) {
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }
        //返回失败
        return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //1.调用业务逻辑层进行分页查询
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        //2.返回相应结果
        return pageResult;
    }
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        //1.调用业务层进行删除
        Boolean flag = checkItemService.delete(id);
        if (flag){
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
    }

    @RequestMapping("/findById.do")
    public Result findById(Integer id) {
        //1.调用业务层进行查询
        CheckItem checkItem = checkItemService.findById(id);
        if (checkItem != null){
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody CheckItem checkItem) {
        //1.调用业务层进行查询
        Boolean flag = checkItemService.edit(checkItem);
        if (flag){
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    @RequestMapping("/findAll.do")
    public Result findAll() {
        //1.调用业务层进行查询
        List<CheckItem> list = checkItemService.findAll();
        if (list != null && list.size() > 0){
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    @RequestMapping("/findCheckItemIdsByCheckGroupId.do")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId) {
        //1.调用业务层进行查询
        Integer[] checkItemIds = checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId);
        if (checkItemIds != null && checkItemIds.length > 0){
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }
}
